package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.TrainingAchievementDetails;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.trainingAchievementDetails.*;
import com.battle.pojo.vo.carWarCase.GetCarWarCaseListVo;
import com.battle.pojo.vo.trainingAchievementDetails.*;
import com.battle.pojo.dto.trainingAchievementDetails.*;
import com.battle.service.repository.in.TrainingAchievementDetailsRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.TrainingAchievementDetailsService;
import com.battle.service.utils.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：个人培训成绩服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TrainingAchievementDetailsServiceImpl implements TrainingAchievementDetailsService {

    private final TrainingAchievementDetailsRepository trainingAchievementDetailsRepository;
    private final UserRepository userRepository;

    public TrainingAchievementDetailsServiceImpl(TrainingAchievementDetailsRepository trainingAchievementDetailsRepository, UserRepository userRepository) {
        this.trainingAchievementDetailsRepository = trainingAchievementDetailsRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建个人培训成绩
     *
     * @param param
     */
    @Override
    public void createTrainingAchievementDetails(CreateTrainingAchievementDetailsParam param) {
        TrainingAchievementDetails trainingAchievementDetails = new TrainingAchievementDetails();
        BeanUtils.copyProperties(param, trainingAchievementDetails);
        Long id = IdGenerator.SNOW.generate();
        trainingAchievementDetails.setId(id);
        trainingAchievementDetailsRepository.create(trainingAchievementDetails);
    }

    /**
     * 修改个人培训成绩
     *
     * @param param
     */
    @Override
    public void editTrainingAchievementDetails(EditTrainingAchievementDetailsParam param) {
        Long id = param.getId();
        TrainingAchievementDetails trainingAchievementDetails = trainingAchievementDetailsRepository.getByIdCheck(id);

        JSONObject jsonObject = param.getBodys();
        //获取总分固定字段 in1
        BigDecimal totalScore = jsonObject.getBigDecimal("in1");
        trainingAchievementDetails.setTotalScore(totalScore);
        trainingAchievementDetails.setDataList(jsonObject.toJSONString());
        trainingAchievementDetailsRepository.edit(trainingAchievementDetails);
    }

    /**
     * 移除个人培训成绩
     *
     * @param id
     */
    @Override
    public void removeTrainingAchievementDetails(Long id) {
        trainingAchievementDetailsRepository.removeById(id);
    }

    /**
     * 启动/禁用个人培训成绩
     *
     * @param id
     */
    @Override
    public void enabledTrainingAchievementDetails(Long id) {
        TrainingAchievementDetails trainingAchievementDetails = trainingAchievementDetailsRepository.getByIdCheck(id);
        trainingAchievementDetails.setIsDisable(!trainingAchievementDetails.getIsDisable());
        trainingAchievementDetailsRepository.edit(trainingAchievementDetails);
    }

    /**
     * 获取个人培训成绩详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTrainingAchievementDetailsVo getTrainingAchievementDetails(Long id) {
        TrainingAchievementDetails trainingAchievementDetails = trainingAchievementDetailsRepository.getByIdCheck(id);
        GetTrainingAchievementDetailsVo vo = new GetTrainingAchievementDetailsVo();
        BeanUtils.copyProperties(trainingAchievementDetails, vo);
        //获取表头
        JSONArray headers = JSONArray.parseArray(trainingAchievementDetails.getHeaderList());
        //获取表体
        String dataList = trainingAchievementDetails.getDataList();
        JSONArray data =new JSONArray();
        data.add(JSONObject.parse(dataList));

        vo.setHeaders(headers);
        vo.setBodys(data);
        return vo;
    }

    /**
     * 获取个人培训成绩 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTrainingAchievementDetailsListVo> getTrainingAchievementDetailsList(GetTrainingAchievementDetailsListParam param) {

        SelectTrainingAchievementDetailsDto dto = new SelectTrainingAchievementDetailsDto();
        BeanUtils.copyProperties(param, dto);
        Page<TrainingAchievementDetails> page = trainingAchievementDetailsRepository.selectTrainingAchievementDetails(dto);
        List<TrainingAchievementDetails> trainingAchievementDetailsList = page.getRecords();
        List<GetTrainingAchievementDetailsListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(trainingAchievementDetailsList)) {
            List<Long> userIds = new ArrayList<>();

            trainingAchievementDetailsList.forEach(v -> {
                userIds.add(v.getCreator());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (TrainingAchievementDetails trainingAchievementDetails : trainingAchievementDetailsList) {
                GetTrainingAchievementDetailsListVo vo = new GetTrainingAchievementDetailsListVo();
                BeanUtils.copyProperties(trainingAchievementDetails, vo);
                User operation = userMap.get(trainingAchievementDetails.getCreator());
                if (operation != null) {
                    vo.setCreateName(operation.getName());
                }
                list.add(vo);
            }
        }
        return new PageResult<>(list, page);

    }

    @Override
    public void export(GetTrainingAchievementDetailsListParam param, HttpServletResponse response) throws IOException {
        param.setSize(Long.MAX_VALUE);
        PageResult<GetTrainingAchievementDetailsListVo> pageResult = getTrainingAchievementDetailsList(param);
        List<GetTrainingAchievementDetailsListVo> list = pageResult.getRecords();
        ExcelUtils.exportExcel(list, "训练考核", "训练考核", GetTrainingAchievementDetailsListVo.class, "训练考核",response);
    }

    /**
     * 获取个人培训成绩下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTrainingAchievementDetailsDownListVo> getTrainingAchievementDetailsDownList(GetTrainingAchievementDetailsDownListParam param) {
        TrainingAchievementDetails po = new TrainingAchievementDetails();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TrainingAchievementDetails> trainingAchievementDetailsList = trainingAchievementDetailsRepository.getListByEntity(po);

        List<GetTrainingAchievementDetailsDownListVo> list = new ArrayList<>();
        for (TrainingAchievementDetails trainingAchievementDetails : trainingAchievementDetailsList) {
            GetTrainingAchievementDetailsDownListVo vo = new GetTrainingAchievementDetailsDownListVo();
            BeanUtils.copyProperties(trainingAchievementDetails, vo);
            list.add(vo);
        }
        return list;
    }


}



